﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace PlayListSet.UI.ValueConverter
{
    internal class DoubleToTimeSpanValueConverter : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double valueDouble = (double)value;
            //Math.Round(num, 3)
            var t = TimeSpan.FromSeconds(valueDouble);
            return $"{t.Hours}:{t.Minutes}:{t.Seconds}.{t.Milliseconds}";
            //TimeSpan.FromSeconds(valueDouble).ToString(@"h\:m\:s\.fff");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            string valueString = (string)value;
            string[] ms = valueString.Split('.');
            var timeArr = ms[0].Split(':');
            TimeSpan timeSpan = new TimeSpan(System.Convert.ToInt32(timeArr[0]), System.Convert.ToInt32(timeArr[1]), System.Convert.ToInt32(timeArr[2]));

            if (ms.Length == 2)
            {
                var m = TimeSpan.FromSeconds(System.Convert.ToDouble("0." + ms[1]));

                timeSpan = timeSpan.Add(m);
            }

            return timeSpan.TotalSeconds;

        }
    }
}
